import json
import os
from log_config import setup_logging

logger = setup_logging()
CONFIG_FILE = "config.json"

def save_config(apply_msg, prefix, file_path, remember, interval_time=15, target_num=20, tag=""):
    """保存配置到文件"""
    try:
        config = {
            "apply_msg": apply_msg,
            "prefix": prefix,
            "file_path": file_path,
            "remember": remember,
            "interval_time": interval_time,
            "target_num": target_num,
            "tag": tag,
        }
        with open(CONFIG_FILE, "w", encoding="utf-8") as f:
            json.dump(config, f)
        logger.info("配置已保存")
    except Exception as e:
        logger.error(f"保存配置失败: {str(e)}")

def load_config():
    """从文件加载配置"""
    try:
        if os.path.exists(CONFIG_FILE):
            with open(CONFIG_FILE, "r", encoding="utf-8") as f:
                config = json.load(f)
                logger.info("配置已加载")
                return config
    except Exception as e:
        logger.error(f"加载配置失败: {str(e)}")
    return None